<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划编号" prop="planCode">
        <el-input v-model="queryParams.planCode" placeholder="请输入计划编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="queryParams.planName" placeholder="请输入计划名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <treeselect v-model="queryParams.deptId" style="width: 150px;" :options="deptOptions" :show-count="true"
          :clearable="false" placeholder="请选择归属部门" :normalizer="normalizer" />

      </el-form-item>
      <el-form-item label="负责人" prop="userId">

        <el-select v-model="queryParams.userId" filterable remote reserve-keyword placeholder="请输入负责人"
          style="width: 150px;" :remote-method="remoteMethod" :loading="userLoading">
          <el-option v-for="item in userList" :key="item.id" :label="item.emqName" :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>

      <el-form-item label="关联设备" prop="imei">
        <el-input v-model="queryParams.imei" placeholder="请输入关联设备" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="是否启用" prop="planEnable">
        <el-select v-model="queryParams.planEnable" placeholder="请选择是否启用">
          <el-option v-for="item in planEnableList" :key="item.value" :label="item.lable" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="审核状态" prop="userId">
        <el-select v-model="queryParams.planState" placeholder="请选择审核状态">
          <el-option v-for="item in stateList" :key="item.value" :label="item.lable" :value="item.value">
          </el-option>
        </el-select>

      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['abu:polling-plan:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          :loading="exportLoading" v-hasPermi="['abu:polling-plan:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">

      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="计划编号" align="center" width="180" prop="planCode">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper" @click="hxySkipPage(scope.row.id,scope.row.ptType)">
            <span style="color: #2196F3;"> {{ scope.row.planCode }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="负责人" align="center" prop="nickname" />
      <el-table-column label="巡检周期" align="center" prop="planPeriodVal">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planPeriodVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务有效时间" align="center" prop="planValidTimeVal" width="180">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planValidTimeVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提前生成时间" align="center" prop="planAdvanceTimeVal" width="180">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planAdvanceTimeVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提前提醒时间" align="center" prop="planRemindTimeVal" width="180">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(scope.row.planRemindTimeVal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划开始时间" align="center" prop="planBeginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planBeginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="planEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次生成时间" align="center" prop="planLastTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planLastTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下次生成时间" align="center" prop="planNextTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planNextTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="planEnable">
        <template slot-scope="scope">
          <span>{{ scope.row.planEnable==1?"是":"否" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前节点" align="center" prop="planNode" />
      <el-table-column label="审批状态" align="center" prop="planState">
        <template slot-scope="scope">
          <span>{{ selectStateLabel(scope.row.planState) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批结束时间" align="center" prop="planApproveEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planApproveEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['abu:polling-plan:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['abu:polling-plan:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="计划名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入计划名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" :clearable="false"
                placeholder="请选择归属部门" :normalizer="normalizer" />

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="userId">
              <el-select v-model="form.userId" filterable remote reserve-keyword placeholder="请输入负责人"
                style="width: 100%;" :remote-method="remoteMethod" :loading="userLoading">
                <el-option v-for="item in userList" :key="item.id" :label="item.emqName" :value="item.id">
                </el-option>
              </el-select>


            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检标准" prop="ptType">
              <el-select v-model="form.ptType" placeholder="请选择巡检标准" style="width: 100%;">
                <el-option v-for="item in standardList" :key="item.id" :label="getLableType(item)" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="巡检周期" prop="planPeriodVal">
              <el-select v-model="form.planPeriodVal" placeholder="请选择巡检周期" style="width: 100%;">
                <el-option v-for="item in dataSectionList" :key="item.id" :label="item.lable" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="有效时间" prop="planValidTimeVal">
              <el-select v-model="form.planValidTimeVal" placeholder="请选择有效时间" style="width: 100%;">
                <el-option v-for="item in dataSectionList" :key="item.id" :label="item.lable" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="提前生成时间" prop="planAdvanceTimeVal">
              <el-select v-model="form.planAdvanceTimeVal" placeholder="请选择提前生成时间" style="width: 100%;">
                <el-option v-for="item in dataSectionList" :key="item.id" :label="item.lable" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="提醒时间" prop="planRemindTimeVal">
              <el-select v-model="form.planRemindTimeVal" placeholder="请选择提前生成时间" style="width: 100%;">
                <el-option v-for="item in dataSectionList" :key="item.id" :label="item.lable" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="计划开始时间" prop="planBeginTime">
              <el-date-picker clearable v-model="form.planBeginTime" type="date" value-format="yyyy-MM-dd"
                style="width: 100%;" placeholder="选择计划开始时间" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="计划结束时间" prop="planEndTime">
              <el-date-picker clearable v-model="form.planEndTime" type="date" value-format="yyyy-MM-dd"
                style="width: 100%;" placeholder="选择计划结束时间" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否启用" prop="planEnable">
              <el-radio-group v-model="form.planEnable">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>

            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    createPollingPlan,
    updatePollingPlan,
    deletePollingPlan,
    getPollingPlan,
    getPollingPlanPage,
    exportPollingPlanExcel,
    getPollingPlanMyList
  } from "@/api/polling/pollingPlan";
  import {
    getUserList,
    getStandardPage
  } from "@/api/polling/standard"
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import Treeselect from "@riophae/vue-treeselect";
  import {
    listSimpleDepts
  } from "@/api/system/dept";
  export default {
    name: "PollingPlan",
    components: {
      Treeselect
    },
    data() {
      return {
        userLoading: false,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 巡检计划列表
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          planCode: null,
          planName: null,
          deptId: null,
          userId: null,
          planEnable: null,
          planState: null,
        },
        // 表单参数
        form: {
          planEnable: 0
        },
        // 表单校验
        rules: {},
        userName: "",
        deptOptions: [],
        userList: [],
        standardList: [],
        stateList: [{
            id: 1,
            value: 0,
            lable: "审核中"
          },
          {
            id: 2,
            value: 1,
            lable: "通过"
          }
        ],
        planEnableList: [{
            id: 1,
            value: 0,
            lable: "否"
          },
          {
            id: 2,
            value: 1,
            lable: "是"
          }
        ],
        dataSectionList: [ //放数据字典
          {
            id: 0,
            lable: "1时"
          },
          {
            id: 1,
            lable: "3时"
          },
          {
            id: 2,
            lable: "1日"
          },
          {
            id: 3,
            lable: "3日"
          },
          {
            id: 4,
            lable: "1周"
          },
          {
            id: 5,
            lable: "1月"
          },
          {
            id: 6,
            lable: "3月"
          },
          {
            id: 7,
            lable: "6月"
          },
          {
            id: 8,
            lable: "12月"
          }
        ]

      };
    },
    created() {
      this.initData();
      this.getList();
    },
    methods: {
      hxySkipPage(item,type) {
        this.$router.push({
          path: '/polling/infoAll',
          query: {
            uid: item,
            tid:type
          }
        })
      },
      selectStateLabel(value) {
        var datas = this.stateList;
        let index = datas.findIndex(item => item.value == value)
        if (index != -1) {
          return datas[index].lable;
        }
        return value;
      },
      selectDictLabel(value) {
        var datas = this.dataSectionList;
        let index = datas.findIndex(item => item.id == value)
        if (index != -1) {
          return datas[index].lable;
        }
        return value;
      },

      getLableType(item) {
        return item.pollingName + "[" + item.pollingCode + "]";
      },
      remoteMethod(query) {
        if (query !== '') {
          this.userName = query
          this.getUserListQuery();
        } else {
          this.userList = [];
        }
      },
      getUserListQuery() {
        this.userLoading = true;
        getUserList({
          equName: this.userName
        }).then(res => {
          this.userLoading = false;
          this.userList = res.data;
          console.log("getUserList", res)
        });
      },

      initData() {
        this.getUserListQuery();
        getStandardPage({
          pageNo: 1,
          pageSize: 50
        }).then(res => {
          this.standardList = res.data.list;
          console.log("getStandardPage", res)
        });
        this.getTreeselect();
      },
      getTreeselect() {
        listSimpleDepts().then(response => {
          // 处理 deptOptions 参数
          this.deptOptions = [];
          this.deptOptions.push(...this.handleTree(response.data, "id"));
        });
      },
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        // 执行查询
        //getPollingPlanPage(this.queryParams).then(response => {
        getPollingPlanMyList(this.queryParams).then(response => {
          this.list = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      /** 取消按钮 */
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 表单重置 */
      reset() {
        this.form = {
          planName: undefined,
          deptId: undefined,
          userId: undefined,
          pType: undefined,
          planValidTime: undefined,
          planAdvanceTime: undefined,
          planBeginTime: undefined,
          planEndTime: undefined,
          planLastTime: undefined,
          planNextTime: undefined,
          planEnable: 0,
          planState: undefined,
          planApproveEndTime: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNo = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加巡检计划";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id;
        getPollingPlan(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改巡检计划";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (!valid) {
            return;
          }
          // 修改的提交
          if (this.form.id != null) {
            updatePollingPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            return;
          }
          // 添加的提交
          createPollingPlan(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.id;
        this.$modal.confirm('是否确认删除巡检计划编号为"' + id + '"的数据项?').then(function() {
          return deletePollingPlan(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        // 处理查询参数
        let params = {
          ...this.queryParams
        };
        params.pageNo = undefined;
        params.pageSize = undefined;
        this.$modal.confirm('是否确认导出所有巡检计划数据项?').then(() => {
          this.exportLoading = true;
          return exportPollingPlanExcel(params);
        }).then(response => {
          this.$download.excel(response, '巡检计划.xls');
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
